package com.maxd.respository.cash;


import com.maxd.model.CashOut;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface CashRepository extends Repository<CashOut, Long> {
    @Query(value = "from CashOut s where s.relationId=:relationId order by s.id desc")
    List<CashOut> findOne(@Param("relationId") String relationId);

    @Query(value = "from CashOut s where s.userId=:id order by s.id desc")
    List<CashOut> selectCashOutList(@Param("id") String id);

    @Query(value = "from CashOut s where s.zhifubao=:zhifubao order by s.id desc")
    List<CashOut> findByPhone(@Param("zhifubao") String zhifubao);

    @Modifying
    @Query(value = "update CashOut s set s.isOut=:isOut where s.id=:id")
    Integer updateIsOut(@Param("isOut") Boolean isOut, @Param("id") Long id);

    @Query(value = "select sum(s.money) from CashOut s where s.relationId=:relationId and s.isOut=true")
    String countByRelationId(@Param("relationId") String relationId);

    @Query(value = "select sum(s.money) from CashOut s where s.userId=:userId and s.isOut=true ")
    String countByUserId(@Param("userId") String userId);

    @Query(value = "select s.relationId from CashOut s group by s.relationId")
    List<Long> findRelationList();

    @Query(value = "select count(s.id) from CashOut s where  s.createAt like concat('%',:time,'%') and s.state=1 ")
    Long getCountNumberByTime(@Param("time") String time);

    @Query(value = "select sum(s.money) from CashOut s where  s.createAt like concat('%',:time,'%') and s.state=1 ")
    String getSumMoneyByTime(@Param("time") String time);

    @Query(value = "select count(s.id) from CashOut s where s.state=0")
    Long getNotOutMoneyByTime();


}